"""
Visual Effects Demo - Test the particle and explosion systems
Run this to see the effects in action without playing the full game
"""
import pygame
import sys
import os

# Add src to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))

from utils.effects import get_effects_manager

def main():
    # Initialize pygame
    pygame.init()
    
    # Create window
    screen = pygame.display.set_mode((800, 600))
    pygame.display.set_caption("Visual Effects Demo")
    clock = pygame.time.Clock()
    
    # Get effects manager
    effects_mgr = get_effects_manager()
    
    # Demo state
    running = True
    last_explosion_time = 0
    explosion_interval = 2000  # ms
    
    # Instructions
    font = pygame.font.Font(None, 24)
    
    print("=== Visual Effects Demo ===")
    print("Controls:")
    print("  LEFT CLICK - Create explosion at mouse position")
    print("  RIGHT CLICK - Create bullet impact at mouse position")
    print("  SPACE - Create random explosions")
    print("  ESC - Exit")
    print()
    print("Watch the particle effects animate!")
    
    while running:
        dt = clock.get_time() / 1000.0
        current_time = pygame.time.get_ticks()
        
        # Handle events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    running = False
                elif event.key == pygame.K_SPACE:
                    # Create random explosions
                    import random
                    for _ in range(3):
                        x = random.randint(100, 700)
                        y = random.randint(100, 500)
                        size = random.choice(["small", "normal", "large"])
                        effects_mgr.create_explosion(x, y, size)
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_x, mouse_y = pygame.mouse.get_pos()
                if event.button == 1:  # Left click
                    effects_mgr.create_explosion(mouse_x, mouse_y, "normal")
                    print(f"Explosion created at ({mouse_x}, {mouse_y})")
                elif event.button == 3:  # Right click
                    effects_mgr.create_bullet_impact(mouse_x, mouse_y)
                    print(f"Bullet impact at ({mouse_x}, {mouse_y})")
        
        # Auto-create explosions
        if current_time - last_explosion_time > explosion_interval:
            import random
            x = random.randint(100, 700)
            y = random.randint(100, 500)
            size = random.choice(["small", "normal", "large"])
            effects_mgr.create_explosion(x, y, size)
            last_explosion_time = current_time
        
        # Update effects
        effects_mgr.update(dt)
        
        # Render
        screen.fill((20, 20, 40))  # Dark blue background
        
        # Draw grid for reference
        for x in range(0, 800, 100):
            pygame.draw.line(screen, (40, 40, 60), (x, 0), (x, 600))
        for y in range(0, 600, 100):
            pygame.draw.line(screen, (40, 40, 60), (0, y), (800, y))
        
        # Render effects
        effects_mgr.render(screen)
        
        # Draw instructions
        instructions = [
            "LEFT CLICK: Explosion | RIGHT CLICK: Impact | SPACE: Random | ESC: Exit",
            f"Active particles: {sum(len(e.particles) for e in effects_mgr.particle_emitters)}",
            f"Active explosions: {len(effects_mgr.explosions)}"
        ]
        
        y_offset = 10
        for text in instructions:
            surface = font.render(text, True, (255, 255, 255))
            screen.blit(surface, (10, y_offset))
            y_offset += 25
        
        # Update display
        pygame.display.flip()
        clock.tick(60)
    
    pygame.quit()
    print("\nDemo ended. Effects system working correctly!")

if __name__ == "__main__":
    main()
